<template>
	<!-- 使用 page-meta 组件配合阻止滚动穿透 ,page-meta一定作为页面的第一个节点-->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>
	<!-- 输入昵称的弹窗 -->
	<uni-popup ref="inputDialog" type="dialog" @change="changePop">
		<uni-popup-dialog ref="inputClose" mode="input" :beforeClose="true" :title="'修改冰箱'+fridge.name+'的昵称'"
			placeholder="请输入内容" :value="fridge.name" @close="close" @confirm="modifyConfirm">
		</uni-popup-dialog>
	</uni-popup>

	<uni-popup ref="sharePopup" type="bottom" @change="changePop" :safeArea="false">
		<my-share-pop :fridge="fridge" @close="closeSharePop"></my-share-pop>
	</uni-popup>

	<view class="detail-container">
		<view class="fridge-box">
			<image :src="fridge.backgroundUrl" style="height: 200rpx; width: 30%;" mode="aspectFit"></image>
			<view class="desc">
				<text class="name-text">{{fridge.name}}</text>
				<view class="brand-model-text">
					<text class="brand-text">{{fridge.brand}}</text>
					<text class="model-text">{{fridge.model}}</text>
				</view>
			</view>
			<view class="member-box" @click="shareToggle">
				<uni-icons type="personadd" size="50" color="#4080ff"></uni-icons>
				<text>邀请成员</text>
			</view>
		</view>

		<view class="modify-box" v-for="(item,index) in op_list" :key="index" @click="clickHandler(index)">
			<text>{{item}}</text>
			<uni-icons type="forward" size="20" style="width: 5%;"></uni-icons>
		</view>

		<view class="default-box">
			<text>设置为默认冰箱</text>
			<switch :checked="fridge.fdefault" :disabled="fridge.fdefault" @change="setDefault" />
		</view>

		<view class="delete-btn">
			<button type="default" @click="deleteFridge">删除</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fridge: {},
				show: false,
				op_list: ['修改昵称', '成员管理', '存取记录', '冰箱偏好设置']
			};
		},

		onLoad(options) {
			this.fridge = JSON.parse(options.fridge)
		},

		onShareAppMessage() {
			this.closeSharePop()
			return {
				title: '你的好友邀请你一起来管理冰箱',
				imageUrl: this.fridge.backgroundUrl,
				path: '/pages/home/home?fid=' + this.fridge.fid
			}
		},

		methods: {

			//跳转
			clickHandler(index) {
				if (index == 0)
					return this.modifyNickname()
				if (index == 1)
					return uni.navigateTo({
						url: '/pagesSub/pageUser/member_manage/member_manage?fridge=' + JSON.stringify(this.fridge)
					})
				if (index == 2)
					return uni.navigateTo({
						url: '/pagesSub/pageFridge/op_record/op_record?fid=' + this.fridge.fid
					})
				if (index == 3)
					return uni.navigateTo({
						url: '/pagesSub/pageFridge/obj_setting/obj_setting?fid=' + this.fridge.fid
					})
			},

			// 请求
			setDefault() {
				uni.$api.fridge.updateDefault(this.fridge.fid)
				this.fridge.fdefault = true
			},
			deleteFridge() {
				// 删除确认
				uni.showModal({
					title: '警告',
					content: `确认删除冰箱${this.fridge.name}？`,
					confirmColor: 'red',
					success: (res) => {
						if (res.confirm) {
							uni.$api.fridge.delete(this.fridge.fid)
							uni.navigateBack({
								delta: 1
							})
						}
					}
				})
			},
			modifyConfirm(nickname) {
				if (this.fridge.name == nickname) {
					return uni.$showMsg('请输入不同的昵称')
				}
				// 修改确认
				uni.showModal({
					title: '提示',
					content: `确认修改冰箱 ${this.fridge.name} 昵称为 ${nickname}?`,
					confirmColor: 'red',
					success: (res) => {
						if (res.confirm) {
							this.fridge.name = nickname
							uni.$api.fridge.update(this.fridge)
							this.$refs.inputDialog.close()
						}
					}
				})
			},

			// 事件处理
			// 修改昵称
			modifyNickname() {
				this.$refs.inputDialog.open()
			},
			close() {
				this.$refs.inputDialog.close()
			},
			changePop(e) {
				this.show = e.show
			},
			// 分享弹窗
			shareToggle() {
				this.$refs.sharePopup.open()
			},
			closeSharePop() {
				this.$refs.sharePopup.close()
			}
		}

	}
</script>

<style lang="scss">
	.detail-container {
		padding-top: 30rpx;
		padding-left: 25rpx;
		padding-right: 25rpx;
		display: flex;
		flex-direction: column;

		.fridge-box {
			display: flex;
			background-color: #fff;
			padding: 15rpx;
			border-radius: 20rpx;
			align-items: center;
			position: relative;

			.desc {
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				width: 40%;
				height: 200rpx;

				.name-text {
					font-size: 45rpx;
				}

				.brand-model-text {
					display: flex;
					flex-direction: column;
					color: #abb2bb;
					font-size: 20rpx;

				}
			}

			.member-box {
				width: 30%;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
			}
		}

		.modify-box,
		.default-box {
			margin-top: 25rpx;
			border-radius: 20rpx;
			height: 100rpx;
			padding-left: 10%;
			font-size: 35rpx;
			padding-right: 20rpx;
			background-color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.delete-btn {
			position: absolute;
			bottom: 10%;
			left: 25rpx;
			right: 25rpx;

			>button {
				background-color: #fbaca3;
				flex: 1;
			}
		}
	}
</style>